package org.csu.wuliu.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonAlias;
import org.csu.wuliu.domain.*;
import org.csu.wuliu.persistence.ItemMapper;
import org.csu.wuliu.persistence.LineItemMapper;
import org.csu.wuliu.persistence.SubstationMapper;
import org.csu.wuliu.persistence.TaskMapper;
import org.csu.wuliu.service.AdminService;
import org.csu.wuliu.service.OrderService;
import org.csu.wuliu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Controller
@ResponseBody
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;


    @GetMapping("/admin/{adminId}")
    public Admin getAdminInfo(@PathVariable String adminId){
        System.out.println(adminId);
        return adminService.getAdminById(adminId);
    }


    //前端返回格式
//    {
//        "adminId": "", "password": ""
//    }
    @PostMapping("/admin/login")
    public Admin signon(@RequestBody String jsonString){
        JSONObject json = JSONObject.parseObject(jsonString);
        Admin admin = new Admin();
        admin.setAdminId(json.getString("adminId"));
        admin.setPassword(json.getString("password"));
        return adminService.getAdminByIdAndPassword(admin);
    }


//    前端请求格式
//    {
//        "orderId": "", "status": ""
//    }
//    返回前端格式

    @PostMapping("/admin/updateOrder")
    public ResponseEntity<String> update(@RequestBody String jsonString){
        JSONObject json = JSONObject.parseObject(jsonString);
        Order order = new Order();
        System.out.println("进入");
        order.setOrderId(json.getString("orderId"));
        order.setStatus(json.getInteger("status"));
        orderService.updateOrder(order);
        return new ResponseEntity<String>("成功", HttpStatus.OK);
    }

//返回前端格式
//    [
//    {
//        "username": "wunanxi",
//            "password": "123",
//            "address": "重庆",
//            "tel": "15736076132"
//    },
//    {
//        "username": "风雪",
//            "password": "123",
//            "address": "贵阳",
//            "tel": "15629518888"
//    }
//]
    @GetMapping("/admin/view/users")
    public List<User> viewUsers(){
        return userService.getUserList();
    }


}
